window.onload=function(){
    // 数据回显ajax
    $.ajax({
        type: "post",
        url: "/js/简介",
        data: null,
        dataType: "json",
        success: function (data1) {
            let showteaobj = JSON.parse(data1);
            let teatypearr = ["绿茶", "黄茶", "乌龙茶", "红茶", "黑茶", "白茶", "花茶"];
            document.getElementsByClassName("bigimg")[0].innerHTML = `<img src="../img/tea-img/${showteaobj[0].teaimg}" alt="">`;
            document.getElementsByClassName("smallimg")[0].innerHTML = `<img src="../img/tea-img/${showteaobj[0].teaimg}" alt="">`;
            document.getElementsByClassName("showteaname")[0].innerHTML = `${showteaobj[0].teaname}`;
            document.getElementsByClassName("font1")[0].innerHTML = `￥${showteaobj[0].teaprice}`;
            document.getElementsByClassName("showteadescribe")[0].innerHTML = `${showteaobj[0].teadescribe}`;
            document.getElementsByClassName("font2")[1].innerHTML = `分类：${teatypearr[showteaobj[0].teatype]}`;

            // 发送ajax请求获取相关的茶
            $.ajax({
                type: "post",
                url: "/js/show",
                data: data1,
                dataType: "json",
                success: function (teaarr) {
                    let teaArr = teaarr;//本地存储
                    let showteaPage = "";
                    let xgsparr = document.getElementsByClassName("xgsp_show")[0];
                    for (let i = 0; i < teaArr.length; i++) {
                        showteaPage += `<div>
                                            <div class="xgsp_show_img">
                                            <img src="../img/tea-img/${teaArr[i].teaimg}" alt="">
                                            </div>
                                            <div class="xgsp_show_name">${teaArr[i].teaname}</div>
                                            <div class="xgsp_show_price">￥${teaArr[i].teaprice}</div>
                                        </div>`;
                    }
                    xgsparr.innerHTML = showteaPage;
                }
            })
            // 添加购物车
            document.getElementById("addShopCar").onclick = function () {
                let shopcar = localStorage.shopCar;
                if (shopcar == "" || shopcar == null || shopcar == undefined || shopcar == []) {
                    localStorage.shopCar = data1;
                    alert("添加至购物车成功");
                    window.location.href = "shopcar.html";
                } else {
                    let bool = false;
                    let shopcararr = JSON.parse(shopcar);
                    for (let i = 0; i < shopcararr.length; i++) {
                        if (shopcararr[i].id == showteaobj[0].id) {
                            bool = true;
                            break;
                        }
                    }
                    if (bool) {
                        alert("该茶叶已添加至购物车");
                    } else {
                        shopcararr.push(showteaobj[0]);
                        localStorage.shopCar = JSON.stringify(shopcararr);
                        alert("添加至购物车成功");
                        window.location.href = "shopcar.html";
                    }
                }
            }
        }
    }) 
}
